package com.yuyou.activity.group.api.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;

import org.hibernate.validator.constraints.NotBlank;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.yuyou.activity.group.service.ActivityGroupSPConfig;
import com.yuyou.activity.group.service.entity.ActivityGroupbuyUser;
import com.yuyou.activity.group.service.service.IActivityGroupSPService;
import com.yuyou.activity.group.service.service.IActivityGroupbuyOrderService;
import com.yuyou.activity.group.service.service.IActivityGroupbuyUserService;
import com.yuyou.activity.group.service.vo.req.ReqActivityGroupSPBase;
import com.yuyou.activity.group.service.vo.req.ReqActivityGroupSPSubOrder;
import com.yuyou.activity.group.service.vo.resp.GroupSpGoodsVo;
import com.yuyou.activity.group.service.vo.resp.RespActivityGroupBuyOrderVo;
import com.yuyou.activity.group.service.vo.resp.RespActivityGroupSPDetail;
import com.yuyou.activity.group.service.vo.resp.RespCalPrice;
import com.yuyou.activity.group.service.vo.resp.RespCanActivityGroupSPList;
import com.yuyou.activity.util.bean.ResBoolSimpleInfo;
import com.yuyou.activity.util.bean.ReturnResult;
import com.yuyou.activity.util.exception.BusinessException;


@Api(value="ActivityGroupSPController",description="shipin.com接口")
@Validated
@RestController
@RequestMapping(value="/api/activityGroupSP")
public class ActivityGroupSPController extends BaseController{
	
	@Autowired
	private IActivityGroupSPService activityGroupSPService;

	@Autowired
	private IActivityGroupbuyUserService activityGroupbuyUserService;

	@Autowired
	private IActivityGroupbuyOrderService activityGroupbuyOrderService;
		
	@ApiOperation(value="商品列表")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "projectCode", value = "项目编号", paramType = "query", required = true)})
	@RequestMapping(value="/findGoods",method = {RequestMethod.GET, RequestMethod.POST})
	public ReturnResult<List<GroupSpGoodsVo>> findGoods(HttpServletRequest request,@Validated @ModelAttribute ReqActivityGroupSPBase base) throws Exception{
		this.valAllClose(base.getProjectCode());

		activityGroupbuyUserService.addOrUpUser(base.getOpenId() , base.getWxImg() , base.getWxUserName() , base.getProjectCode());

		List<GroupSpGoodsVo> list = activityGroupSPService.findGoods(base.getProjectCode());
		return ReturnResult.success(list);
	}
	
	@ApiOperation(value="推荐拼团列表更多")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "limit", value = "最大数量(1-10)", paramType = "query", required = true)})
	@RequestMapping(value="/findMoreCanActivityGroup",method = {RequestMethod.GET, RequestMethod.POST})
	public ReturnResult<List<RespCanActivityGroupSPList>> findMoreCanActivityGroup(HttpServletRequest request,
			@Validated @ModelAttribute ReqActivityGroupSPBase base,
			@NotNull(message="最大数量不能为空")Integer limit) throws Exception{
        if(limit.intValue()<1 && limit.intValue()>10){
            throw new BusinessException("limit参数不正确");
        }

		this.valAllClose(base.getProjectCode());

		activityGroupbuyUserService.addOrUpUser(base.getOpenId() , base.getWxImg() , base.getWxUserName() , base.getProjectCode());


		List<RespCanActivityGroupSPList> list = activityGroupSPService.findMoreCanActivityGroupBuy(base.getProjectCode() , limit);
        return ReturnResult.success(list);
	}
	
	@ApiOperation(value="提交订单")
	@RequestMapping(value="/submitOrder",method = {RequestMethod.POST})
	public ReturnResult<String> submitOrder(
			HttpServletRequest request,
			@Validated @ModelAttribute ReqActivityGroupSPSubOrder order) throws Exception{		
		this.valAllClose(order.getProjectCode());	
		
		//参团，开团时间控制
		if(ActivityGroupSPConfig.getConfig(order.getProjectCode()).getJoinClose().getTime()<new Date().getTime()){
			throw new BusinessException("不能参团开团");
		}
		
		ResBoolSimpleInfo<String> res=activityGroupSPService.submitOrder(order);
		return ReturnResult.returnRes(res);
	}
		
	@ApiOperation(value="开团情况")
	@ApiImplicitParams({})
	@RequestMapping(value="/actGroDetById",method = {RequestMethod.GET, RequestMethod.POST})
	public ReturnResult<RespActivityGroupSPDetail> actGroDetById(
			@Validated @ModelAttribute ReqActivityGroupSPBase base ,
			@NotNull (message="ID") Long activityGroupId) throws Exception{
		this.valAllClose(base.getProjectCode());

		activityGroupbuyUserService.addOrUpUser(base.getOpenId() , base.getWxImg() , base.getWxUserName() , base.getProjectCode());

		ResBoolSimpleInfo<RespActivityGroupSPDetail> res = activityGroupSPService.activityGroupSPDetailById(base.getProjectCode() , activityGroupId , base.getOpenId());

		return ReturnResult.success(res.getData());
	}
	

	@ApiOperation(value="我的订单")
	@ApiImplicitParams({})
	@RequestMapping(value="/myOrder",method = {RequestMethod.GET, RequestMethod.POST})
	public ReturnResult<List<RespActivityGroupBuyOrderVo>> myOrder(
			@Validated @ModelAttribute ReqActivityGroupSPBase base) throws Exception{
		this.valAllClose(base.getProjectCode());

		activityGroupbuyUserService.addOrUpUser(base.getOpenId() , base.getWxImg() , base.getWxUserName() , base.getProjectCode());

		List<RespActivityGroupBuyOrderVo> list = activityGroupbuyOrderService.myOrder(base.getProjectCode() , base.getOpenId());

		return ReturnResult.success(list);
	}

    @ApiOperation(value="查询价格(单位：分)")
    @ApiImplicitParams({
			@ApiImplicitParam(name = "groupType", value = "groupType", paramType="query", required = true),
			@ApiImplicitParam(name = "ids", value = "ids", paramType="query", required = true)})
    @RequestMapping(value="/getPrice",method = {RequestMethod.GET, RequestMethod.POST})
    public ReturnResult<RespCalPrice> getPrice(
            @Validated @ModelAttribute ReqActivityGroupSPBase base ,
            @NotNull(message="groupType不能为空")  Integer groupType ,
            @NotEmpty(message = "商品") Long[] ids) throws Exception{

        this.valAllClose(base.getProjectCode());

        activityGroupbuyUserService.addOrUpUser(base.getOpenId() , base.getWxImg() , base.getWxUserName() , base.getProjectCode());

        Set<Long> set = new HashSet<>();
        for (Long id : ids) {
           set.add(id);
        }
        RespCalPrice respCalPrice = activityGroupSPService.calculate(base.getProjectCode(),groupType , set);

        return ReturnResult.success(respCalPrice);
    }
	
    @ApiOperation(value="获取用户的头像")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = "projectCode", value = "projectCode", paramType="query", required = true),
        @ApiImplicitParam(name = "openId", value = "openId", paramType="query", required = true)
    })
    @RequestMapping(value ="/img",method = {RequestMethod.GET, RequestMethod.POST})
    public ReturnResult<String> img(@NotBlank (message="openId不能为空")  String openId,@NotBlank (message="projectCode不能为空") String projectCode) throws Exception {
        this.valAllClose(projectCode);
        ActivityGroupbuyUser user = activityGroupbuyUserService.selectByOpenId(projectCode , openId);
        if(user == null){
            return ReturnResult.fail("找不到该用户！");
        }

        return ReturnResult.success(user.getHeadImg());
    }
	
	
}
